﻿using IOP.SgrA;
using IOP.SgrA.ECS;
using IOP.SgrA.Input;
using System;
using System.Collections.Generic;
using System.Text;

namespace VkSample47
{
    public class InputSystem : ComponentSystem
    {
        private readonly IGraphicsManager RenderManager;

        private IRenderGroup[] RenderGroups = new IRenderGroup[3];
        private int index;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="contextManager"></param>
        public InputSystem(IGraphicsManager renderManager)
        {
            RenderManager = renderManager;
        }

        public override void Initialization()
        {
            for(int i =0; i < RenderGroups.Length; i++)
            {
                RenderManager.TryGetRenderGroup($"SGroup{i}", out IRenderGroup renderGroup);
                RenderGroups[i] = renderGroup;
            }
        }

        public override void Update(TimeSpan timeSpan)
        {
            if (Input.GetMouseButtonState(MouseButton.ButtonLeft).Action == InputAction.Click)
            {
                RenderGroups[index++].Disable();
                var mask = index % 3;
                RenderGroups[mask].Enable();
                index = mask;
            }
            ContextManager.Foreach<Group>((group) =>
            {
                Context context = ContextManager.GetContext(group.Entity.Index);
                IRenderGroup renderGroup = context.GetContextRenderGroup();
                if(renderGroup.IsEnable()) context.PushToRenderGroup();
            });
        }
    }

    public struct Group
    {
        public Entity Entity;
        public TransformComponent Transform;
    }
}
